home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / Apple Script / OSAX / Four Password OSAXen / Password1 Folder / PassWord.c < prev    next >
C/C++ Source or Header  |  1993-05-05  |  3KB  |  133 lines

  1. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. //    Password.c
  4. //    Written by: Donald Olson
  5. //    May 1993
  6. //    Copyright ® 1993 Apple Computer Inc.
  7. //     All rights reserved.
  8. //
  9. //    This is a simple Scripting Addition that asks the user to type in a
  10. //    password.  This was written for a talk at the 1993 WWDC given
  11. //     by Donald Olson and Donn Denman.
  12. //
  13. //    Syntax: User Password
  14. //    Return: Encrypted form of user password or cancel.
  15. //
  16. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  17.  
  18.  
  19. #include <string.h>
  20. #include <Dialogs.h>
  21. #include <AppleEvents.h>
  22. //AEVTOLIEpswd
  23.  
  24. // Our headers
  25. void        FrameDefault( DialogPtr theDialog, long theItem);
  26. pascal     Boolean MyDlgFilter(    DialogPtr theDlg, 
  27.                             EventRecord *theEventRec, short *itemHit);
  28. void     EncryptString(StringPtr strPtr);
  29.  
  30. // Our constant
  31. #define DLOGID 128
  32.  
  33. pascal OSErr main(AppleEvent *theEvent, 
  34.                 AppleEvent *theReply, 
  35.                 long theRefCon) 
  36. {
  37.     
  38.     DialogPtr         theDialog = nil;
  39.     OSErr            theErr = noErr;
  40.     GrafPtr            savedPort;
  41.     short            itemHit;
  42.     Str32            theString;
  43.     StringPtr        theStrPtr = (StringPtr)&theString;
  44.     StringHandle    theStrHdl = (StringHandle)&theStrPtr;
  45.     
  46.     GetPort(&savedPort);
  47.     
  48.     theErr = AEInteractWithUser(kAEDefaultTimeout, nil, nil);
  49.     if(theErr != noErr) return theErr;
  50.     
  51.     theDialog = GetNewDialog(DLOGID, nil, (WindowPtr)-1);    
  52.         
  53.         if(theDialog == nil) return resNotFound;        
  54.        
  55.         SetPort(theDialog);
  56.     FrameDefault(theDialog, ok);
  57.     
  58.     strcpy((char*)theString, (char*) "\0");         // Empty string
  59.     
  60.     /*     
  61.         Use window refCon to pass user input back to this routine
  62.         since the text field will have our bullets instead of the
  63.         actual text entered.
  64.     */
  65.     
  66.     SetWRefCon(theDialog, (long)theStrHdl);    
  67.     
  68.     do {
  69.         ModalDialog((ModalFilterProcPtr)MyDlgFilter, &itemHit);
  70.     } while (itemHit != cancel && itemHit != ok);
  71.      
  72.      if(itemHit == cancel) {
  73.          DisposDialog(theDialog);
  74.          return userCanceledErr;
  75.      }
  76.     
  77.     DisposDialog(theDialog);
  78.     
  79.     EncryptString(theStrPtr);
  80.     
  81.     theErr = AEPutParamPtr(theReply, keyDirectObject, typeChar, 
  82.                             (Ptr)theString, strlen((char*)theString));
  83.     SetPort(savedPort);
  84.     return theErr;
  85. }
  86.  
  87. void EncryptString(StringPtr strPtr) {
  88.     short    theSize, x;
  89.     theSize = strlen((char*)(strPtr));
  90.     
  91.     for( x = 0; x <= (theSize -1); x++) {
  92.         strPtr[x] = (short)strPtr[x] + (short)"\¡";
  93.     }
  94. }
  95.  
  96. pascal Boolean MyDlgFilter(DialogPtr theDlg, EventRecord *theEventRec, short *itemHit) {    
  97.     
  98.     StringHandle    theStrHdl;
  99.     short            theSize;
  100.     if(theEventRec->what != keyDown) // Just looking for keystrokes
  101.         return false;
  102.             
  103.     switch((theEventRec->message) & charCodeMask)
  104.     {
  105.         case 0x0d:                // Return
  106.             *itemHit = ok;      
  107.             return true;
  108.         case 0x03:                // Enter
  109.             *itemHit = ok;      
  110.             return true;
  111.         case 0x08:                // Backspace
  112.             return false;
  113.         default:
  114.             theStrHdl = (StringHandle)GetWRefCon(theDlg);
  115.             theSize = strlen((char*)(*theStrHdl));
  116.             (*theStrHdl)[theSize] = (char)(theEventRec->message);
  117.             (*theStrHdl)[theSize + 1] = (char)NULL;
  118.             theEventRec->message = '•';
  119.             return false;
  120.     }
  121. }
  122.  
  123. void FrameDefault( DialogPtr theDialog, long theItem) {
  124.     short    DType; 
  125.     Handle    DItem;
  126.     Rect    DRect;
  127.     
  128.     GetDItem(theDialog, theItem, &DType, &DItem, &DRect); 
  129.     PenSize(3, 3); 
  130.     InsetRect(&DRect, -4, -4);
  131.     FrameRoundRect(&DRect, 16, 16);
  132.     PenSize(1, 1);
  133. }